package com.jose.photogallery;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import com.jose.photogallery.domain.PhotogalleryDAO;
import com.jose.photogallery.domain.Photo;
import com.jose.photogallery.domain.UploadUtil;



@SuppressWarnings("serial")
public class BlobstoreUploadServlet extends HttpServlet {
	private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	private BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		PhotogalleryDAO dao = new PhotogalleryDAO();
		
		Date uploadDate = new Date();
		
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		
		Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
		BlobKey blobKey = blobs.get("photo");
		BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(blobKey);
		System.out.println("BlobInfo: " + blobInfo.getContentType());
		System.out.println("BlobInfo: " + blobInfo.getFilename());
		
		if (UploadUtil.isValidFileType(blobInfo.getContentType())){
			// Get request parameters
			String caption = (String) req.getParameter("caption");
			List<String> tags = UploadUtil.splitInput(req.getParameter("tags"));
			String description = (String) req.getParameter("description");
			String privacy = (String) req.getParameter("privacy");
			
			// Create image serving URL
			ImagesService imagesService = ImagesServiceFactory.getImagesService();
			String imageURL = imagesService.getServingUrl(blobKey);
			
			//Image image = ImagesServiceFactory.makeImageFromBlob(blobKey);
			
			// Create the photo object
			Photo photo = new Photo();
			photo.setUser(user.getUserId());
			photo.setBlobKey(blobKey.getKeyString());
			photo.setImageURL(imageURL);
			//photo.setHeight(image.getHeight());
			//photo.setWidth(image.getWidth());
			photo.setCaption(caption);
			photo.setDescription(description);
			photo.setTags(tags);
			photo.setPrivacy(privacy);
			photo.setUploadDate(uploadDate);
			photo.setUpdateDate(uploadDate);
			photo.setRating(0);
			photo.setCumulative(0);
			photo.setVotes(0);
			
			dao.savePhoto(photo);
			
			// Show the new uploaded Photo
			resp.sendRedirect("/photo?key=" + photo.getBlobKey());
		} else {
			//Delete the file from blobstore and redirect the user to an error page
			blobstoreService.delete(blobKey);
			
			resp.sendRedirect("/error");
		}
	}

}
